package com.xxxx.seckill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.seckill.VO.LoginVO;
import com.xxxx.seckill.VO.ResponseBean;
import com.xxxx.seckill.VO.ResponseBeanEnum;
import com.xxxx.seckill.exception.GlobalException;
import com.xxxx.seckill.mapper.UserMapper;
import com.xxxx.seckill.pojo.User;
import com.xxxx.seckill.service.IUserService;
import com.xxxx.seckill.utils.CookieUtil;
import com.xxxx.seckill.utils.MD5Util;
import com.xxxx.seckill.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.thymeleaf.util.StringUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Jack
 * @since 2024-01-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public ResponseBean doLogin(LoginVO loginVO, HttpServletRequest request, HttpServletResponse response) {
        String mobile = loginVO.getMobile();
        String password = loginVO.getPassword();
        // 参数校验
//        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
//            return ResponseBean.error(ResponseBeanEnum.LOGIN_ERROR);
//        }
//        if (!ValidatorUtil.isMobile(mobile)) {
//            return ResponseBean.error(ResponseBeanEnum.LOGIN_ERROR);
//        }
        // 根据手机号查找用户
        User user = userMapper.selectById(mobile);
        if (null == user) {
            throw new GlobalException(ResponseBeanEnum.LOGIN_ERROR);
        }
        // 判断密码是否正确
        if (!MD5Util.formPassToDBPass(password, user.getSalt()).equals(user.getPassword())) {
            throw new GlobalException(ResponseBeanEnum.LOGIN_ERROR);
        }
        // 生成Cookie
        String userTicket = UUIDUtil.uuid();
        // 把用户信息放在了Session里面，现在不要这么做了，所以注释掉
        // request.getSession().setAttribute(userTicket, user);
        // 将用户信息存入Redis中
        redisTemplate.opsForValue().set("user:" + userTicket, user);
        CookieUtil.setCookie(request, response, "userTicket", userTicket);
        // 登录成功
        return ResponseBean.success(userTicket); // 注意：参数不能少，因为要生成几千个用户 userTicket ，需要从 response 中获取 userTicket ，将其写入 object 中方便获取
    }

    @Override
    public User getUserByCookie(String ticket, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isEmpty(ticket)) {
            return null;
        }
        User user = (User) redisTemplate.opsForValue().get("user:" + ticket);
        if (null != user) {
            CookieUtil.setCookie(request, response, "userTicket", ticket);
        }
        return user;
    }

    @Override
    public ResponseBean updatePassword(String userTicket, String password, HttpServletRequest request, HttpServletResponse response) {
        User user = getUserByCookie(userTicket, request, response);
        if (null == user) {
            throw new GlobalException(ResponseBeanEnum.MOBILE_NOT_EXISTS);
        }
        user.setPassword(MD5Util.inputPassToDBPass(password, user.getSalt()));
        int result = userMapper.updateById(user);
        if (1 == result) {
            // 一定要记得删除 Redis 中的过时信息
            redisTemplate.delete("user:" + userTicket);
            return ResponseBean.success(userTicket);
        }
        return ResponseBean.error(ResponseBeanEnum.UPDATE_PASSWORD_FAILURE);
    }


}